package com.dut.bbs.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

/**
 * 基础Activity，实现了累计所有未关闭Activity的功能，并记录当前显示的Activity。
 * 
 */
public class BaseActivity extends Activity {

	public static final String LOG_TAG = "BaseActivity";

	private static Activity currentActivity;
	
	private static List<Activity> activityList = new ArrayList<Activity>();
	
	public BaseActivity() {
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addActivity(this);
		setCurrentActivity(this);
	}

	@Override
	protected void onResume() {
		setCurrentActivity(this);
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		removeActivity(this);
		super.onDestroy();
	}

	protected Context getContext() {
		return this;
	}
	
	private static boolean addActivity(Activity activity) {
		boolean flag = activityList.add(activity);
		//Log.i(LOG_TAG, "addActivity " + activity.getClass().getSimpleName() + activityList.size());
		return flag;
	}
	
	private static boolean removeActivity(final Activity activity) {
		boolean flag = activityList.remove(activity);
		//Log.i(LOG_TAG, "removeActivity " + activity.getClass().getSimpleName() + activityList.size());
		return flag;
	}
	
	/**
	 * 设置当前Activity实例
	 * 
	 * @param activity
	 * @return
	 */
	private static void setCurrentActivity(Activity activity) {
		if (activity != null) {
			Log.i(LOG_TAG, "currentActivity=" + activity.getClass());
		}
		BaseActivity.currentActivity = activity;
	}

	/**
	 * 获得当前显示的Activity。
	 * 
	 * @return
	 */
	public static Activity getCurrentActivity() {
		return currentActivity;
	}
	/**
	 * 获得所有未关闭的活动（Activity）。
	 * @return
	 */
	public static List<Activity> getActivities() {
		/*
		 * 拷贝一个List对象作为返回结果，一方面为了防止外部修改内部List数据，
		 * 另一方面防止内部的修改影响到外部调用。
		 */
		List<Activity> copyActivityList = new ArrayList<Activity>(activityList);
		return copyActivityList;
	}
	
	/**
	 * 关闭程序。
	 */
	public static void exit() {
		List<Activity> activities = getActivities();
		for (Activity activity : activities) {
			if (activity != null) {
				activity.finish();
				Log.i(LOG_TAG, "Activity " + activity.getClass() + " is finished!!!");
			}
		}
		android.os.Process.killProcess(android.os.Process.myPid());
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (getCurrentActivity().getClass().equals(LoginActivity.class)) {
				dialog_Exit(this);
			} else {
				finish();
			}
			return true;
		} else {
			super.onKeyDown(keyCode, event);
			return false;
		}
	}

	public static void dialog_Exit(Context context) {

		AlertDialog.Builder builder = new Builder(context);
		builder.setMessage("确定要退出吗?");
		builder.setTitle("提示");
		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setPositiveButton("确认",
		new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
//				getCurrentActivity().finish();
//				android.os.Process.killProcess(android.os.Process.myPid()); 
				exit();
			}

		});
		builder.setNegativeButton("取消",
		new android.content.DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}
	
	/**
	 * private防止不初始化
	 */
	private ProgressDialog progressDialog = null;
	/**
	 * @return ProgressDialog, title:请稍候
	 */
	public ProgressDialog getProgressDialog() {
		if (progressDialog == null) {
			progressDialog = new ProgressDialog(getContext());
			progressDialog.setTitle("请稍候");  
		}
		return progressDialog;
	}
}